﻿using System;
using System.Collections.Generic;
using System.Linq;
using FootballProject.DAL.DatatAccessLib;

namespace FootballProject.BL.BusinessManagers
{
    /// <summary>
    /// Author: Ivan Shikht
    /// </summary>
    class MatchManager
    {
        #region Fields
        private readonly EntitiesContext _context;
        #endregion

        #region Constructors
        public MatchManager()
        {
            _context = new EntitiesContext();
        }
        #endregion

        #region Methods
        public List<Match> FindAll()
        {
            IQueryable<Match> rMatch = from match in _context.MatchSet
                                       select match;
            return rMatch.ToList();
        }

        public List<Match> FindMatchesByAwayTeam(AwayTeam awayTeam)
        {
            IQueryable<Match> rMatch = from match in _context.MatchSet
                                       where match.AwayTeams == awayTeam
                                       select match;
            return rMatch.ToList();
        }

        public List<Match> FindMatchesByHomeTeam(HomeTeam homeTeam)
        {
            IQueryable<Match> rMatch = from match in _context.MatchSet
                                       where match.HomeTeams == homeTeam
                                       select match;
            return rMatch.ToList();
        }

        public List<Match> FindMatchesByDivision(Division division)
        {
            IQueryable<Match> rMatch = from match in _context.MatchSet
                                       where match.Divisions == division
                                       select match;
            return rMatch.ToList();
        }

        public List<Match> FindMatchesByReferee(Referee referee)
        {
            IQueryable<Match> rMatch = from match in _context.MatchSet
                                       where match.Referees == referee
                                       select match;
            return rMatch.ToList();
        }

        public List<Match> FindMatchesByMatchDate(DateTime dateTime)
        {
            IQueryable<Match> rMatch = from match in _context.MatchSet
                                       where match.MatchDate == dateTime
                                       select match;
            return rMatch.ToList();
        }

        public Match FindMatchById(Guid matchId)
        {
            IQueryable<Match> rMatch = from match in _context.MatchSet
                                       where match.MatchID == matchId
                                       select match;
            return rMatch.First();
        }

        public void UpdateMatch(Match match)
        {
            _context.Attach(match);
            match.SetAllModified(_context);
            _context.SaveChanges();
        }

        public void AddMatch(Match match)
        {
            _context.AddToMatchSet(match);
            _context.SaveChanges();
        }


        public void DeleteMatch(Match match)
        {
            _context.Attach(match);
            _context.DeleteObject(match);
            _context.SaveChanges();
        }
        #endregion
    }
}
